﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 动态组件信息
    /// </summary>
    /// <remarks>对 SketchUpAPI 中 SUDynamicComponentInfoRef 的包装</remarks>
    public class DynamicComponentInfo : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="dc_info"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDynamicComponentInfoRelease(ref IntPtr dc_info);

        /// <summary>
        /// 动态属性个数
        /// </summary>
        /// <param name="dc_info"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDynamicComponentInfoGetNumDCAttributes(IntPtr dc_info, out int count);

        /// <summary>
        /// 全部属性
        /// </summary>
        /// <param name="dc_info"></param>
        /// <param name="len"></param>
        /// <param name="attributes"><see cref="DynamicComponentAttribute"/></param>
        /// <param name="realcount"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDynamicComponentInfoGetDCAttributes(IntPtr dc_info, int len, IntPtr[] attributes, out int realcount);
        #endregion

        /// <summary>
        /// 属性
        /// </summary>
        public List<DynamicComponentAttribute> Attributes
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDynamicComponentInfoGetNumDCAttributes(Id, out int count));
                IntPtr[] pts = new IntPtr[count];
                SketchUpExceptionHelper.ThrowSUResult(SUDynamicComponentInfoGetDCAttributes(Id, count, pts, out _));
                return pts.Select(i => new DynamicComponentAttribute(i)).ToList();
            }
        }

        internal DynamicComponentInfo(IntPtr id) => Id = id;

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUDynamicComponentInfoRelease(ref Id));
        }
    }
}